package be.xios.projectquiz.player;

import java.io.IOException;

import javax.swing.JFrame;

import be.xios.projectquiz.game.Blokken;
import be.xios.projectquiz.game.BlokkenPlayerGUI;
import be.xios.projectquiz.game.Game;
import be.xios.projectquiz.game.Player;
import be.xios.projectquiz.game.SharedAntwoord;
import be.xios.projectquiz.game.SharedBlokkenBoolean;
import be.xios.projectquiz.global.CardsViewer;
import be.xios.projectquiz.global.ScreenMethods;
import be.xios.projectquiz.global.StreamMethods;

public class PlayerClientBlokken implements Runnable {

	private BlokkenPlayerGUI blokkenGUI; 
	private StreamMethods streamMethods;
	private Game currentGame;
	private Player currPlayer;
	private SharedAntwoord sharedAntwoord = new SharedAntwoord();
	private SharedBlokkenBoolean sharedBoolean = new SharedBlokkenBoolean();
	private CardsViewer viewer;
	
	public PlayerClientBlokken(StreamMethods streamMethods, Game currentGame, Player currPlayer) {
		this.streamMethods=streamMethods;
		this.currentGame=currentGame;
		this.currPlayer=currPlayer;
	}
	
	
	@Override
	public void run() {
		ScreenMethods main = new ScreenMethods();
		blokkenGUI = new BlokkenPlayerGUI(currentGame, currPlayer, sharedAntwoord, sharedBoolean);
		blokkenGUI.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		blokkenGUI.setSize(main.getFullScreenX(), main.getFullScreenY());
		blokkenGUI.setUndecorated(false);
		blokkenGUI.setVisible(true);
		
		this.viewer=blokkenGUI.getViewer();
		
		//WHILE BLOKKEN.GETCURRENTROUND = 1
		while(((Blokken)currentGame).isRonde1() ) {	
			if(blokkenGUI.getUpdatedGame() != null && sharedBoolean.isFirst()){
				try {
					//VERSTUREN VAN PLAYEROBJECT NAAR DE SERVER
					System.err.println(Thread.currentThread() + " " + currPlayer.getNaam() + " is sending");
					streamMethods.openObjectOutputStream();
					streamMethods.getObjectOutput().writeObject(currPlayer);
		
					System.err.println("Waiting for server to send updated game");
					
					//ONTVANGEN VAN NIEUW GAMEOBJECT MET PLAYERLIST VAN DE SERVER
					streamMethods.openObjectInputStream();
					blokkenGUI.setUpdatedGame((Blokken)streamMethods.getObjectInput().readObject());
					
					System.err.println(currPlayer.getNaam() + " heeft een updated game met playerlist ontvangen.");
					
					if ( currPlayer.getNaam().equals(blokkenGUI.getUpdatedGame().getKlikVolgOrde().get(0).getNaam())) {
						//Antwoord invullen en versturen
						blokkenGUI.getSolpanel().setVisible(true);
					} 
					else {
						//wachten totdat de speler het antwoord ingevuld heeft en de jury verbeterd heeft.
						viewer.show("LPWAIT");
					}	
					//blokkenGUI.setUpdatedGame(null);	
				} 
				catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} 
			}//Einde if
			
			//Antwoord wat de speler heeft ingevuld terug doorsturen naar de Server.
			try {
				Thread.sleep(10);
				if (sharedAntwoord.getAntwoord() != null) {
					
					streamMethods.openObjectOutputStream();
					streamMethods.getObjectOutput().writeObject(sharedAntwoord.getAntwoord());
					
					System.out.println("CLIENTSIDE: player sent antwoord " + sharedAntwoord.getAntwoord().getAntwoord());
				}
				//Destroyen van antw nadat het succesvol verzonden is
				sharedAntwoord.setAntwoord(null);
			}
			catch (InterruptedException e1) {
				e1.printStackTrace();
			}
			catch (IOException e) {
				e.printStackTrace();
			}
		}//Einde ronde 1

		
		//WHILE BLOKKEN CURRENT ROUND = 2
		
		//WHILE BLOKKEN CURRENT ROUND = 3
		
		//FINALE RONDE
	}

}
